using HKB.Common;
using System;
using System.Data;
using System.IO;
using System.Text;
using System.Web;
namespace HKB.Web
{
	public class SkinBll
	{
		private Conn conn = null;
		public SkinBll()
		{
			this.conn = Text.GetComponent<Conn>("conn", HttpContext.Current.CurrentHandler);
		}
		public SkinBll(Conn _conn)
		{
			this.conn = _conn;
		}
		public bool AddSkin(string SkinName, string Skin_CSS, bool IsDefault, bool IsDefaultInProject, string ProjectName, string SkinType, string SkinDir, string SkinPath, string SkinPreviewImg)
		{
			bool result;
			if (Skin_CSS != null)
			{
				if (!this.ModifySkinFile(SkinType, 0, IsDefault, "Add", Skin_CSS, ProjectName, SkinDir + SkinPath))
				{
					result = false;
					return result;
				}
			}
			if (IsDefault)
			{
				IDbDataParameter[] parameters = new IDbDataParameter[]
				{
					this.conn.CreateParameter("@ProjectName", DbType.String, ProjectName),
					this.conn.CreateParameter("@SkinType", DbType.String, SkinType)
				};
				this.conn.Execute("UpDate [HL_Skin] Set IsDefault=0 Where (ProjectName=@ProjectName and SkinType=@SkinType)", parameters);
			}
			IDbDataParameter[] parameters2 = new IDbDataParameter[]
			{
				this.conn.CreateParameter("@SkinName", DbType.String, SkinName),
				this.conn.CreateParameter("@IsDefault", DbType.Boolean, IsDefault),
				this.conn.CreateParameter("@IsDefaultInProject", DbType.Boolean, IsDefaultInProject),
				this.conn.CreateParameter("@ProjectName", DbType.String, ProjectName),
				this.conn.CreateParameter("@SkinType", DbType.String, SkinType),
				this.conn.CreateParameter("@SkinPath", DbType.String, SkinPath),
				this.conn.CreateParameter("@SkinPreviewImg", DbType.String, SkinPreviewImg)
			};
			int num = this.conn.Execute("Insert Into [HL_Skin](SkinName,IsDefault,IsDefaultInProject,ProjectName,SkinType,SkinPath,SkinPreviewImg) Values(@SkinName,@IsDefault,@IsDefaultInProject,@ProjectName,@SkinType,@SkinPath,@SkinPreviewImg)", parameters2);
			result = (num == 1);
			return result;
		}
		public bool UpateSkin(string SkinName, string Skin_CSS, bool IsDefault, string ProjectName, int SkinID, string SkinType, string SkinDir, string SkinPath, string SkinPreviewImg)
		{
			if (!SkinPath.Contains(".css"))
			{
				SkinPath = SkinPath + "/" + ProjectName + ".css";
			}
			bool result;
			if (Skin_CSS != null)
			{
				if (!this.ModifySkinFile(SkinType, SkinID, IsDefault, "Update", Skin_CSS, ProjectName, SkinDir + SkinPath))
				{
					result = false;
					return result;
				}
			}
			IDbDataParameter[] parameters = new IDbDataParameter[]
			{
				this.conn.CreateParameter("@SkinName", DbType.String, SkinName),
				this.conn.CreateParameter("@IsDefault", DbType.Boolean, IsDefault),
				this.conn.CreateParameter("@ProjectName", DbType.String, ProjectName),
				this.conn.CreateParameter("@SkinType", DbType.String, SkinType),
				this.conn.CreateParameter("@SkinPath", DbType.String, SkinPath),
				this.conn.CreateParameter("@SkinPreviewImg", DbType.String, SkinPreviewImg),
				this.conn.CreateParameter("@SkinID", DbType.Int32, SkinID)
			};
			int num = this.conn.Execute("Update [HL_Skin] Set SkinName=@SkinName,IsDefault=@IsDefault,ProjectName=@ProjectName,SkinType=@SkinType,SkinPath=@SkinPath,SkinPreviewImg=@SkinPreviewImg Where ([SkinID] = @SkinID)", parameters);
			if (IsDefault)
			{
				this.SetIsDefault(SkinID);
			}
			result = (num == 1);
			return result;
		}
		protected bool ModifySkinFile(string SkinType, int SkinID, bool IsDefault, string Action, string Skin_Css, string ProjectName, string SkinPath)
		{
			bool flag = true;
			string text = HttpContext.Current.Server.MapPath("~/") + "Skin\\" + SkinPath;
			bool result;
			if (Action != "Del")
			{
				FileInfo fileInfo = new FileInfo(text);
				if (!fileInfo.Directory.Exists)
				{
					try
					{
						fileInfo.Directory.Create();
					}
					catch
					{
						result = false;
						return result;
					}
				}
				try
				{
					using (StreamWriter streamWriter = new StreamWriter(text, false, Encoding.GetEncoding("UTF-8")))
					{
						streamWriter.WriteLine(Skin_Css);
						streamWriter.Flush();
						streamWriter.Close();
					}
					result = true;
					return result;
				}
				catch
				{
					result = false;
					return result;
				}
			}
			if (Action == "Del" && flag && File.Exists(text))
			{
				FileInfo fileInfo = new FileInfo(text);
				fileInfo.Delete();
			}
			result = true;
			return result;
		}
		public bool DeleteSkin(int SkinID)
		{
			IDbDataParameter dbDataParameter = this.conn.CreateParameter("@SkinID", DbType.Int32, SkinID);
			int num = this.conn.Execute("Delete From [HL_Skin] Where ([SkinID] = @SkinID)", new IDbDataParameter[]
			{
				dbDataParameter
			});
			return num == 1;
		}
		public bool DeleteSkinByProName(string ProjectName)
		{
			IDbDataParameter dbDataParameter = this.conn.CreateParameter("@ProjectName", DbType.String, ProjectName);
			return this.conn.Execute("Delete From [HL_Skin] Where ([ProjectName] = @ProjectName)", new IDbDataParameter[]
			{
				dbDataParameter
			}) >= 1;
		}
		public bool SetIsDefaultInProject(string ProjectName)
		{
			IDbDataParameter dbDataParameter = this.conn.CreateParameter("@ProjectName", DbType.String, ProjectName);
			return this.conn.Execute("Update [HL_Skin] Set IsDefaultInProject=0;Update [HL_Skin] Set IsDefaultInProject=1 Where ([ProjectName] = @ProjectName)", new IDbDataParameter[]
			{
				dbDataParameter
			}) >= 0;
		}
		public bool BakProjectSkin(string ProjectName, string NewProjectName)
		{
			DataTable skinByProjectName = this.GetSkinByProjectName(ProjectName);
			for (int i = 0; i < skinByProjectName.Rows.Count; i++)
			{
				IDbDataParameter[] parameters = new IDbDataParameter[]
				{
					this.conn.CreateParameter("@SkinName", DbType.String, skinByProjectName.Rows[i]["SkinName"]),
					this.conn.CreateParameter("@IsDefault", DbType.Boolean, skinByProjectName.Rows[i]["IsDefault"]),
					this.conn.CreateParameter("@IsDefaultInProject", DbType.Boolean, false),
					this.conn.CreateParameter("@ProjectName", DbType.String, NewProjectName),
					this.conn.CreateParameter("@SkinType", DbType.String, skinByProjectName.Rows[i]["SkinType"]),
					this.conn.CreateParameter("@SkinPath", DbType.String, skinByProjectName.Rows[i]["SkinPath"]),
					this.conn.CreateParameter("@SkinPreviewImg", DbType.String, skinByProjectName.Rows[i]["SkinPreviewImg"])
				};
				this.conn.Execute("Insert Into [HL_Skin](SkinName,IsDefault,IsDefaultInProject,ProjectName,SkinType,SkinPath,SkinPreviewImg) Values(@SkinName,@IsDefault,@IsDefaultInProject,@ProjectName,@SkinType,@SkinPath,@SkinPreviewImg)", parameters);
			}
			return true;
		}
		public bool SetIsDefault(int SkinID)
		{
			IDbDataParameter dbDataParameter = this.conn.CreateParameter("@SkinID", DbType.Int32, SkinID);
			int num = this.conn.Execute("UpDate [HL_Skin] Set IsDefault=0 Where (ProjectName=(Select ProjectName From [HL_Skin] Where SkinID=@SkinID) and SkinType=(Select SkinType From [HL_Skin] Where SkinID=@SkinID));UpDate [HL_Skin] Set IsDefault=1 Where  ([SkinID] = @SkinID)", new IDbDataParameter[]
			{
				dbDataParameter
			});
			return num >= 1;
		}
		public DataTable GetSkinBySkinID(int SkinID)
		{
			IDbDataParameter dbDataParameter = this.conn.CreateParameter("@SkinID", DbType.Int32, SkinID);
			return this.conn.GetDataTable("Select * From [HL_Skin] Where ([SkinID] = @SkinID)", new IDbDataParameter[]
			{
				dbDataParameter
			});
		}
		public DataTable GetAllSkin()
		{
			return this.conn.GetDataTable("Select * From [HL_Skin] Order By SkinID Desc");
		}
		public DataTable GetSkinBySkinType(string SkinType)
		{
			IDbDataParameter dbDataParameter = this.conn.CreateParameter("@SkinType", DbType.String, SkinType);
			return this.conn.GetDataTable("Select * From [HL_Skin] Where ([SkinType] = @SkinType)", new IDbDataParameter[]
			{
				dbDataParameter
			});
		}
		public DataTable GetProSkinBySkinType(string SkinType)
		{
			IDbDataParameter dbDataParameter = this.conn.CreateParameter("@SkinType", DbType.String, SkinType);
			return this.conn.GetDataTable("Select * From [HL_Skin] Where (IsDefaultInProject=1 and ([SkinType] = @SkinType))", new IDbDataParameter[]
			{
				dbDataParameter
			});
		}
		public DataTable GetDefaultBySkinType(string SkinType)
		{
			IDbDataParameter dbDataParameter = this.conn.CreateParameter("@SkinType", DbType.String, SkinType);
			return this.conn.GetDataTable("Select * From [HL_Skin] Where (IsDefault=1 and ([SkinType] = @SkinType))", new IDbDataParameter[]
			{
				dbDataParameter
			});
		}
		public DataTable GetProDefaultBySkinType(string SkinType)
		{
			IDbDataParameter dbDataParameter = this.conn.CreateParameter("@SkinType", DbType.String, SkinType);
			return this.conn.GetDataTable("Select * From [HL_Skin] Where (IsDefaultInProject=1 and IsDefault=1 and ([SkinType] = @SkinType))", new IDbDataParameter[]
			{
				dbDataParameter
			});
		}
		public DataTable GetSkinByProjectName(string ProjectName)
		{
			IDbDataParameter dbDataParameter = this.conn.CreateParameter("@ProjectName", DbType.String, ProjectName);
			return this.conn.GetDataTable("Select * From [HL_Skin] Where ([ProjectName] = @ProjectName)", new IDbDataParameter[]
			{
				dbDataParameter
			});
		}
		public DataTable GetSkinByProjectName()
		{
			return this.conn.GetDataTable("Select * From [HL_Skin] Where (IsDefaultInProject = 1)");
		}
	}
}
